<template>
  <div class="order-list">
    <el-empty v-if="orders.length === 0" description="暂无订单" />
    
    <div v-else class="orders">
      <el-card v-for="order in orders" :key="order.id" class="order-card">
        <template #header>
          <div class="order-header">
            <div class="order-info">
              <span class="order-id">订单号：{{ order.id }}</span>
              <span class="order-date">{{ formatDate(order.createdAt) }}</span>
            </div>
            <el-tag :type="getStatusType(order.status)">
              {{ getStatusText(order.status) }}
            </el-tag>
          </div>
        </template>
        
        <div class="order-items">
          <div v-for="item in order.items" :key="item.id" class="order-item">
            <img :src="item.image" class="item-image" />
            <div class="item-info">
              <h4>{{ item.name }}</h4>
              <p class="item-price">¥{{ item.price }} × {{ item.quantity }}</p>
            </div>
          </div>
        </div>
        
        <div class="order-footer">
          <div class="order-total">
            总计：<span class="price">¥{{ order.total.toFixed(2) }}</span>
          </div>
          
          <div class="order-actions">
            <el-button
              v-if="order.status === 'pending'"
              type="primary"
              @click="handlePay(order.id)"
            >
              立即付款
            </el-button>
            <el-button
              v-if="order.status === 'pending'"
              type="danger"
              @click="handleCancel(order.id)"
            >
              取消订单
            </el-button>
            <el-button
              v-if="order.status === 'paid'"
              type="success"
              @click="handleConfirm(order.id)"
            >
              确认收货
            </el-button>
          </div>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { ElMessageBox, ElMessage } from 'element-plus'
import { useOrderStore } from '../stores/order'

const props = defineProps({
  orders: {
    type: Array,
    required: true
  }
})

const orderStore = useOrderStore()

const formatDate = (date) => {
  return new Date(date).toLocaleString()
}

const getStatusType = (status) => {
  const types = {
    pending: 'warning',
    paid: 'primary',
    completed: 'success',
    cancelled: 'info'
  }
  return types[status] || 'info'
}

const getStatusText = (status) => {
  const texts = {
    pending: '待付款',
    paid: '已付款',
    completed: '已完成',
    cancelled: '已取消'
  }
  return texts[status] || status
}

const handlePay = async (orderId) => {
  try {
    await ElMessageBox.confirm('确认支付该订单？', '提示', {
      type: 'warning'
    })
    await orderStore.payOrder(orderId)
    ElMessage.success('支付成功')
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('支付失败，请重试')
    }
  }
}

const handleCancel = async (orderId) => {
  try {
    await ElMessageBox.confirm('确认取消该订单？', '提示', {
      type: 'warning'
    })
    await orderStore.cancelOrder(orderId)
    ElMessage.success('订单已取消')
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('取消失败，请重试')
    }
  }
}

const handleConfirm = async (orderId) => {
  try {
    await ElMessageBox.confirm('确认已收到商品？', '提示', {
      type: 'warning'
    })
    await orderStore.completeOrder(orderId)
    ElMessage.success('确认收货成功')
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('操作失败，请重试')
    }
  }
}
</script>

<style scoped>
.order-list {
  padding: 20px 0;
}

.order-card {
  margin-bottom: 20px;
}

.order-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.order-info {
  display: flex;
  gap: 20px;
}

.order-date {
  color: #666;
}

.order-items {
  margin: 20px 0;
}

.order-item {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 10px 0;
  border-bottom: 1px solid #eee;
}

.order-item:last-child {
  border-bottom: none;
}

.item-image {
  width: 80px;
  height: 80px;
  object-fit: cover;
  border-radius: 4px;
}

.item-info {
  flex: 1;
}

.item-price {
  color: #666;
  margin-top: 5px;
}

.order-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid #eee;
}

.order-total {
  font-size: 16px;
}

.price {
  color: #f56c6c;
  font-size: 20px;
  font-weight: bold;
}

.order-actions {
  display: flex;
  gap: 10px;
}
</style> 